<?php
declare(strict_types = 1);
namespace app\admin\model;
use think\Config;
use think\Model;
class Brand extends Model{
    public function getRecommendedAttr($value){
        if (\think\facade\Config::has('view.par_goods_Recommended')){
            $config = \think\facade\Config::get('view.par_goods_Recommended');
            return $config[$value];
        }
    }
    public function getIfShowAttr($value){
        if (\think\facade\Config::has('view.par_goods_if_show')){
            $config = \think\facade\Config::get('view.par_goods_if_show');
            return $config[$value];
        }
    }
    public function searchKeywordsAttr($query,$v){
        if (''!==$v){
            $query->where('ecm_brand.brand_name','like','%'.$v.'%');
        }
    }
    public function searchBrandLogoAttr($query,$v){
        if ($v=='0'){
           $query->where('brand_logo','=','')->whereNull("brand_logo");
        }
        if ($v=='1'){
            $query->whereNotNull("brand_logo");
        }
    }
    public function searchIfShowAttr($q,$v){
        if (isset($v) && is_scalar($v) && '2'!=$v){
            $q->where('ecm_brand.if_show',strval($v));
        }
    }
    public static function getBrand($data = [],$query = [],$page=true){
    $model = self::field(true);
//        if($data['keywords']!=''){
//            $model->where("brand_name like '%{$data['keywords']}%'");
//        }


//        if ($data['if_show']!=''){
//            $model->where("if_show={$data['if_show']}");
//        }
        $model ->withSearch(array_keys($data),$data);

        if ($page){
            $list_rows= \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows'=>  $list_rows,
                'query'    => $query,
            ]);
        }else{
            return $model->select();
        }
    }
}